<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">Music</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-button type="primary" @click="tj">添加分类</el-button>
      </el-row>
      <tree-table :data='list' :columns='columns' :selection-type='false' :expand-type='false' show-index index-text="#" border class="treeTable">
           <!-- 是否有效 -->
           <template slot="isok" slot-scope='scope'>
               <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color: lightgreen;"></i>
                <i class="el-icon-error" v-if="scope.row.cat_deleted===true" style="color: red;"></i>
           </template>
           <!-- 排序 -->
           <template slot="order" slot-scope='scope'>
               <el-tag v-if="scope.row.cat_level===0">一级</el-tag>
               <el-tag type='success' v-else-if="scope.row.cat_level===1">二级</el-tag>
               <el-tag type='warning' v-else>三级</el-tag>
           </template>
           <!-- 操作 -->
            <template slot="opt" slot-scope='scope'>
               <el-button type="primary" icon="el-icon-edit" @click="editbtn(scope.row.cat_id)">编辑</el-button>
               <el-button type="danger" icon="el-icon-delete" @click="delbtn(scope.row.cat_id)">删除</el-button>
            </template>
       </tree-table>
    </el-card>
    <el-dialog
  title="添加"
  :visible.sync="addCateDialogVisible"
  width="50%" 
  @close="addclose"
  >
  <!-- 添加分类表单 -->
  <el-form :model="addCateForm"  ref="addCateFormRef" label-width="100px">
  <el-form-item label="分类名称" prop="cat_name">
    <el-input v-model="addCateForm.cat_name"></el-input>
  </el-form-item>
   <el-form-item label="父级分类">

  <el-cascader
  expand-trigger="hover"
    v-model="selectedKeys"
    :options="catelist"
    :props="cascaderProps"
    @change="parentCateChanged" clearable change-on-select></el-cascader>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="addCateDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addCate">确 定</el-button>
  </span>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog
  title="提示"
  :visible.sync="edit"
  width="30%"
  >
  <el-form :model="ruleForm">
    <el-form-item label="分类名称" prop="name">
    <el-input v-model="ruleForm.cat_name"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="edit = false">取 消</el-button>
    <el-button type="primary" @click="dial">确 定</el-button>
  </span>
</el-dialog>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[2, 5, 7, 10]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { getcartlist, getsp, addsp,getspp,putspp,delspp } from '@/api/user'
import {Message} from 'element-ui'
export default {
  methods: {
   async delbtn(id) {
      await delspp(id)
      Message.success('删除成功')
   this.getlist()
    },
   async dial() {
     let res =  await putspp(this.ruleForm,this.ruleForm.cat_id)
      console.log(res);
      this.edit = false
     Message.success('更新成功')
     this.getlist()
    },
    async editbtn(id) {
   
      let res = await getspp(id)
      console.log(res);
      this.edit = true
     this.ruleForm = res.data
    },
    addclose() {
      this.$refs.addCateFormRef.resetFields()
      this.selectedKeys = [],
        this.addCateForm.cat_level = 0,
      this.addCateForm.cat_pid = 0
    },
   async getspbtn() {
   let res =  await  getsp({type:'2'})
      console.log(res);
   this.catelist = res.data
    },
    tj() {
      this.getspbtn()
      this.addCateDialogVisible = true
    },
    parentCateChanged() {
      if (this.selectedKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
     this.addCateForm.cat_level = this.selectedKeys.length
     return
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level =0
     }
    },
  async  addCate() {
    let res =   await addsp(this.addCateForm)
  
    this.addCateDialogVisible = false
    Message.success('添加成功')
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getlist()
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getlist()
    },
    async getlist() {
     let res = await getcartlist(this.queryInfo)
     this.list = res.data.result
      this.total = res.data.total
     console.log(res);
    }
  },
  created() {
  this.getlist()
  },
data() {
  return {
    ruleForm: {
      cat_name:''
    },
    edit:false,
    selectedKeys:[],
    cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
    catelist:[],
    addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
    addCateDialogVisible:false,
    total: 0,
    columns: [{
        label: '分类名称',
        prop: 'cat_name'
      },
      // 将当前列定义为模板列
      {
        label: '是否有效',
        type: 'template',
        template: 'isok'
      },
      {
        label: '排序',
        type: 'template',
        template: 'order'
      },
      {
        label: '操作',
        type: 'template',
        template: 'opt'
      }
      ],
    list: [],
    total:0,
    queryInfo: {
      type: 3,
    pagenum: 1,
    pagesize:5
   }
  }
},
}
</script>

<style>

</style>